/**
 * js
 */
angular.module('mall.payment', [
        "ngStorage"
    ])

    .config(['$stateProvider', '$urlRouterProvider',
        function ($stateProvider, $urlRouterProvider) {
            $stateProvider
                .state('morepay', {
                    url: '/morepay/:orderid',
                    templateUrl: templateprefix + 'pay/morepay.html',
                    params: {
                        order: null
                    },
                    controller: 'MorepayController'
                })
            ;
        }])

    //支付页面控制器
    .controller('MorepayController', ['$scope', '$state', '$window', '$stateParams', 'OrderService', 'ShopcartService','MallAuthService','PayService','PayConfig',
        function ($scope, $state, $window, $stateParams, OrderService,ShopcartService, MallAuthService,PayService,PayConfig) {
            $scope.environment = BrowserType();
            //paymentpassword();//支付密码输入框设置
            $scope.mall_enturl = projectBaseUrl;//回跳地址
            ShopcartService.synchronizationStorage();
            //MallAuthService.Userbalance().$promise.then(function (data) {
            //    $scope.paysecret = data.paysecret;
            //}, function (error) {
            //    //空
            //});
            $scope.order = null;
            $scope.way = "";
            //选择支付方式
            function choose_pay() {
                var payWay = $('input:radio:checked').val();
                if (payWay == 1) {
                    $scope.way = 'wx';
                } else if (payWay == 2) {
                    $scope.way = 'ali';
                } else if (payWay == 3) {
                    $scope.way = 'yue';
                }
            }

            //默认支付方式
            if ($scope.environment) {
                $('#paythree1').attr("checked", "checked")
            } else {
                $('#paythree2').attr("checked", "checked")
            }

            //获取订单支付信息。
            //if ($stateParams.order != null) {
            //    $scope.order = OrderService.getOrderById($stateParams.order.orderid);
            //    OrderService.cacheOrderid($stateParams.order.orderid);
            //    OrderService.delcache();
            //} else if ($stateParams.orderid != '' && $stateParams.order == null) {
            //    $scope.order = OrderService.getOrderById($stateParams.orderid);
            //    OrderService.cacheOrderid($stateParams.orderid);
            //    OrderService.delcache();
            //} else {
            //    var orderid = OrderService.getCacheOrederid();
            //    $scope.order = OrderService.getOrderById(orderid);
            //    OrderService.delcache();
            //}
            if ($stateParams.order != null) {
                $scope.orderid = $stateParams.order.orderid;
                OrderService.cacheOrderid($stateParams.order.orderid);
                OrderService.delcache();
            } else if ($stateParams.orderid != '' && $stateParams.order == null) {
                $scope.orderid = $stateParams.orderid;
                OrderService.cacheOrderid($stateParams.orderid);
                OrderService.delcache();
            } else {
                $scope.orderid = OrderService.getCacheOrederid();
                OrderService.delcache();
            }
            //确认下单
            /*$scope.place_order = function () {
                choose_pay();
                if ($scope.way == "") {
                    alert("请选择支付方式");
                } else {
                    if (!$scope.order.buyMessage) {
                        $scope.order.buyMessage = "农归吧订单";
                    } else if ($scope.way == "yue") {
                        if ($scope.paysecret == null || $scope.paysecret == '') {
                            input_alert("支付密码未设置", 1000);
                            setTimeout(function () {
                                $window.location.href = projectBaseUrl + "setpassword";
                            }, 1000);
                        } else {
                            $(".payment-pages").css({opacity:1,zIndex:1});
                            $(".payment_keyboard").css({transform: "translateY(0px)"}, {"-webkit-transform": "translateY(0px)"});
                        }
                    }
                    $scope.wapPay($scope.order);
                }
            };*/
            $scope.place_order = function () {
                choose_pay();
                if ($scope.way == "") {
                    alert("请选择支付方式");
                } else {
                    if ($scope.way == "yue") {
                        window.location.href = jumpurl($scope.orderid, "", 102);
                    }
                    if ($scope.way == "ali") {
                        window.location.href = jumpurl($scope.orderid, "","");
                    }
                    if ($scope.way == "wx") {
                        var wxopenid = MallAuthService.getWxopenid();
                        window.location.href = jumpurl($scope.orderid, wxopenid,"");
                    }
                }
            };
            //支付调用
            $scope.wapPay = function (order_info) {
                if ($scope.way == "wx") {//微信支付
                    if (appEnvironment == false) {
                        WxpayService.jspiPay(order_info, function(result){
                                WeixinJSBridge.invoke('getBrandWCPayRequest', {
                                    "appId": result.appID,//"wx2421b1c4370ec43b", //公众号名称，由商户传入
                                    "timeStamp": result.timeStamp,//"1395712654", //时间戳，自1970年以来的秒数
                                    "nonceStr": result.nonceStr,//"e61463f8efa94090b1f366cccfbbb444", //随机串
                                    "package": result.pacKage,//"prepay_id=u802345jgfjsdfgsdg888",
                                    "signType": result.signType,//"MD5", //微信签名方式:
                                    "paySign": result.paySign,//"70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名
                                }, function (res) { // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                                    alert(JSON.stringify(res));
                                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                                        OrderService.delcacheid();
                                        $window.location.href = projectBaseUrl + "orderlist/2";
                                    }
                                    if (res.err_msg == "get_brand_wcpay_request:cancel") {
                                        alert("交易取消");
                                    }
                                    if (res.err_msg == "get_brand_wcpay_request:fail") {
                                        alert("支付失败");
                                    }
                                });
                        });
                    }
                    else if (appEnvironment == true) {
                        function success_func(result) {
                            Wechat.sendPaymentRequest({
                                    "appid": result.appID,//"wx2421b1c4370ec43b", //公众号名称，由商户传入
                                    "partnerid": result.partnerid,
                                    "prepayid": result.prepayid,
                                    "timestamp": result.timeStamp,//"1395712654", //时间戳，自1970年以来的秒数
                                    "noncestr": result.nonceStr,//"e61463f8efa94090b1f366cccfbbb444", //随机串
                                    "package": result.pacKage,//"prepay_id=u802345jgfjsdfgsdg888",
                                    "sign": result.paySign,//"70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名
                                }, function (res) { // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                                        OrderService.delcacheid();
                                        $window.location.href = projectBaseUrl + "orderlist/2";
                                    }
                                    if (res.err_msg == "get_brand_wcpay_request:cancel") {
                                        alert("交易取消");
                                    }
                                    if (res.err_msg == "get_brand_wcpay_request:fail") {
                                        alert("支付失败");
                                    }
                                },
                                function (reason) {
                                    console.log(JSON.stringify(reason));
                                }
                            );
                        }

                        function error(result) {
                            console.log(result);
                        }

                        WxpayService.appPay(order_info, success_func, error);
                    }
                }
                if ($scope.way == "ali") {//阿里支付
                    if (appEnvironment == false) {
                        AliWapPay.webPay(order_info).then(function (data) {
                            $window.location.href = data;
                        });
                    } else if (appEnvironment == true) {
                        var order_info = order_info;
                        AliWapPay.aliAppPay(order_info, function (reason) {
                            AliWapPay.AppPay(reason, order_info);
                        }, function (reason) {
                            console.log(reason)
                        })
                    }
                }
            };
            $scope.paymentchange = function(){
                $scope.paymentpassword = $("#paymentTxt").val();
                if($scope.paymentpassword.length==6){
                    $scope.order.paysecret = $scope.paymentpassword;
                    YuepayService.yuepay($scope.order,
                        function () {
                            input_alert("支付完成", 1000);
                            OrderService.delcacheid();
                            setTimeout(function () {
                                $window.location.href = projectBaseUrl + "orderlist/2";
                            }, 1000);
                        }, function (data) {
                            if (data.data.reason == "请设置支付密码！") {
                                input_alert(data.data.reason, 1000);
                                setTimeout(function () {
                                    $window.location.href = projectBaseUrl + "setpassword";
                                }, 1000);
                            }
                            input_alert(data.data.reason, 1000);
                            setTimeout(function () {
                                $("#pwd-input").focus();
                                $("#pwd-input").select();
                            }, 1000);
                        });
                }
            };
            paymentNum($scope.paymentchange);
        }])
;
